/**
 * problem_026.cpp
 * Copyright (C) 2011 - xrose
 */

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string mDivn(int m, int n, int *rec);     //in: m, n; out:rec --> recursive
int main (int argc, char const* argv[])
{
    string res;
    int rec;
    int mCnt = 0;
    int mD = 0;

//    res = mDivn(1, 7, &rec);
//    cout<<"result = "<<res<<endl;
//    cout<<"rec-->"<<rec<<endl;

    for(int i = 1; i<1000; i++)
    {
        res = mDivn(1, i, &rec);
//        cout<<"result = "<<res<<endl;
//        cout<<"rec-->"<<rec<<endl;
        if(mCnt < rec)
        {
            mD = i;
            mCnt = rec;
        }
    }
    cout << "Max = "<<mD<<"max fraction recur: "<< mCnt<<endl;
    return 0;
}

string mDivn(int m, int n, int *rec)
{
    // Implement m/n; return to a string
    int check[1000] = {0};
    int cnt = 0;
    char c;
    int ret = 0;
    string result;
    std::stringstream ss;

    while(m>=n)
    {
        cnt++;
        m=m-n;
    }
    ss<< cnt;
    result+=ss.str();
    result+='.';

    if(m==0)
    {
        *rec = 0;
        return result;
    } else
    {
        ret = 0;
        while((ret==0)&&(m<n))
        {
            m*=10;
            cnt = 0;
            while(m>=n)
            {
                m=m-n;
                cnt++;
            }
            c = cnt + 48;
            result+=c;
            if((m!=0)&&(check[m]==0))
            {
                check[m]=1;
                ret = 0;
            } else
            {
                ret = 1;
            }
        }
        cnt = 0;
        for(int i = 0; i<n; i++)
        {
//            cout<<"check["<<i<<"]"<<check[i]<<endl;
            if(check[i]) cnt++;
        }
        *rec = cnt;
        return result;
    }
}
